package com.project.lk.nurseapp.base;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;


import com.project.lk.nurseapp.utils.OkHttpUtils;
import com.project.lk.nurseapp.utils.UrlEncryptUtils;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public abstract class PostBaseModel<T> {
    protected ModelPresenterBridge bridge;
    private boolean succeed;
    private boolean failusre;
    protected Handler handler = new Handler();
    public void getJsonData(final String url, RequestBody body) {

        OkHttpUtils.postJsonAsync(url, body, new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
//                       Toast.makeText(BaseApp.getInstance(),"网络异常，请检查网络",Toast.LENGTH_LONG).show();
                        postFailedOnMainThread(url);
                        handler.removeCallbacks(this);
                    }
                });

            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {

                final String json = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        List<T> list = analyzeJson(json, url);
                        postSuccessOnMainThread(list, url);
                        handler.removeCallbacks(this);
                    }
                });
            }
        });
    }
    public void getJsonData(final String url, RequestBody body, Context context) {

        OkHttpUtils.postJsonAsync(url, body, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("MLOG",e.toString());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                       Toast.makeText(BaseApp.getInstance(),"服务器异常",Toast.LENGTH_LONG).show();
                       postFailedOnMainThread(url);
                       handler.removeCallbacks(this);

                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                final String json = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        handler.removeCallbacks(this);
                        final List<T> list = analyzeJson(json, url);
                        postSuccessOnMainThread(list, url);

                    }
                });
            }
        });
    }

    public abstract List<T> analyzeJson(String json, String url);

    public abstract void postSuccessOnMainThread(List<T> list,String url);


    public void setBridge(ModelPresenterBridge bridge) {
        this.bridge = bridge;
    }

    public abstract void postFailedOnMainThread(String url);

    public String  postSignToken(String currentMillis,String nonce){
        return   UrlEncryptUtils.getSignature(BaseApi.token,currentMillis, nonce);

    }


}
